package com.buttonpay.settings;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.buttonpay.DataHelper;
import com.buttonpay.Login;
import com.buttonpay.MyProducts;
import com.buttonpay.R;
import com.buttonpay.balance.BalanceTab;

public class SettingList extends Activity  
{
	ListView settings;
	Button btnBack,btnLogout;
	TextView tveaderText;
	String[] values = new String[] 
			{
	          	"Change Login Password","Change Transaction PIN","Update Products"
	    	};
	Integer[] images=new Integer[]
			{
				R.drawable.change_password,
				R.drawable.change_transaction_pin,
				R.drawable.update_product
			};
 
	private ProgressDialog pd=null;
	
	private Handler handler=new Handler();
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
	}
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_list);
        settings=(ListView)findViewById(R.id.lvSettingsList);
        
        btnBack=(Button)findViewById(R.id.btnPrevious);
        btnLogout=(Button)findViewById(R.id.btnLogout);
        
        tveaderText=(TextView)findViewById(R.id.tv_headerText);
        tveaderText.setText("Settings");
             
        MyListAdapter adapter = new MyListAdapter(this, values,images);
        settings.setAdapter(adapter);
        
        settings.setOnItemClickListener(new OnItemClickListener()
        {

			public void onItemClick(AdapterView<?> adapterView, View view, int position,
					long id)
			{
				switch (position)
				{
					case 0:
						new ChangeLoginDailog(SettingList.this).show();	
						break;
						
					case 1:
						new ChangeTransactionPin(SettingList.this).show();
						break;
						
					case 2:
						pd =ProgressDialog.show(SettingList.this,null, "Updating Products..", true);
						pd.setContentView(R.layout.progress);
						new Thread(UpdateProducts).start();
						   
						break;
				}
					 
			}
        	
		});
        btnBack.setOnClickListener(new OnClickListener()
		{
			
			public void onClick(View arg0) 
			{
				// TODO Auto-generated method stub
				finish();
			}
			
		});
		
		btnLogout.setOnClickListener(new OnClickListener()
		{
			
			public void onClick(View v) 
			{
				new AlertDialog.Builder(SettingList.this)
		        .setTitle( "Exit Application" )
		        .setMessage( "Are you sure you want to Logout?" )
		        .setPositiveButton("YES", new android.content.DialogInterface.OnClickListener() 
		        {
		            public void onClick(DialogInterface arg0, int arg1) 
		            {
		                //do stuff onclick of YES
		            	setResult(2);
		            	finish();
		            }

					public void onClick(View v) 
					{
						// TODO Auto-generated method stub
						
					}
		        })
		        .setNegativeButton("NO",  new android.content.DialogInterface.OnClickListener() {
		            public void onClick(DialogInterface arg0, int arg1)
		            {
		                //do stuff onclick of CANCEL
		            	arg0.dismiss();
		               
		            }

					public void onClick(View v) 
					{
						// TODO Auto-generated method stub
					}
		        }).show();
				  		
			}
		});
    
    }//oncreate
	Runnable UpdateProducts=new Runnable()
    {
		
		public void run() 
		{
			// TODO Auto-generated method stub
			DataHelper db=new DataHelper(SettingList.this);
			db.updateDB();
			
			MyProducts p=new MyProducts(SettingList.this, pd, handler);
			p.CheckProducts();
			db.close();
			pd.dismiss();
			setResult(1);
		}
	};
	
	 public static class ViewHolder
	 {
			public TextView tvItemName;
			public ImageView ivItemImage;
			 
     }
	 class MyListAdapter extends ArrayAdapter<String> 
	 {
			
			private final Context context;
			private final String[] values;
			private final Integer[] images;
			LayoutInflater vi ;
		  
			public MyListAdapter(Context context, String[] values, Integer[] images)
			{
				super(context, R.layout.row_lv_settings,values);
				
				this.context = context;
				this.values = values; 
				this.images=images;
				vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			}
 
			public View getView(final int position, View convertView, ViewGroup parent) 
			{
				 View v = convertView;
				 ViewHolder holder = null;
				 
				if (v == null) 
				{
						holder=new ViewHolder();  
						v = vi.inflate(R.layout.row_lv_settings, null); 
						holder.tvItemName=(TextView) v.findViewById(R.id.tvItemName);
						holder.ivItemImage=(ImageView)v.findViewById(R.id.ivOperatorIcon);
						v.setTag(holder); 
					 
				}
				else
					holder=(ViewHolder)v.getTag();
					String name=this.values[position];
					System.out.println(name);
					holder.tvItemName.setText(name);
					holder.ivItemImage.setImageResource(this.images[position]);
					  
				 	return v; 
			}
		}	
	 
}
